package com.wxd.modules.function.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxd.annotation.DataFilter;
import com.wxd.modules.function.dao.AddEmpDao;
import com.wxd.modules.function.entity.AddEmp;
import com.wxd.modules.function.entity.EmpEntity;
import com.wxd.modules.function.entity.department.JWDEmpEntity;
import com.wxd.modules.function.entity.department.JWDEntity;
import com.wxd.modules.function.entity.department.KYDEmpEntity;
import com.wxd.modules.function.service.AddEmpService;
import com.wxd.modules.function.service.EmpService;
import com.wxd.modules.function.service.RewardRecordService;
import com.wxd.modules.function.service.WorkExperienceService;
import com.wxd.modules.function.service.department.JwdService;
import com.wxd.modules.function.service.department.KydService;
import com.wxd.modules.sys.entity.SysDeptEntity;
import com.wxd.modules.sys.entity.SysUserEntity;
import com.wxd.modules.sys.service.SysDeptService;
import com.wxd.utils.Constant;
import com.wxd.utils.PageUtils;
import com.wxd.utils.Query;
import com.wxd.utils.R;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service("addEmpService")
public class AddEmpServiceImpl extends ServiceImpl<AddEmpDao, AddEmp> implements AddEmpService {

    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private EmpService empService;
    @Autowired
    private WorkExperienceService workExperienceService;
    @Autowired
    private RewardRecordService rewardRecordService;
    @Autowired
    private JwdService jwdService;
    @Autowired
    private KydService kydService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @DataFilter(subDept = true, user = false)
    public PageUtils queryPage(Map<String, Object> params) {
        String dateStart=(String)params.get("dateStart");
        String dateEnd=(String)params.get("dateEnd");
        String deptIdStr= (String)params.get("deptId");
        Long deptIdLong;
        //根据部门ID将部门下的所有人员查找出来
        List<Long> deptIdList = null;
        if(StringUtils.isNotBlank(deptIdStr))
        {
            deptIdLong= Long.valueOf(deptIdStr);
            deptIdList =sysDeptService.getSubDeptIdList(deptIdLong);
            deptIdList.add(deptIdLong);
        }
        IPage<AddEmp> page = this.page(
                new Query<AddEmp>().getPage(params),
                new QueryWrapper<AddEmp>().between(StringUtils.isNotBlank(dateStart),"date",dateStart,dateEnd).in(deptIdList!=null,"dept_id",deptIdList)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );
        for(AddEmp addEmp :page.getRecords())
        {
            EmpEntity empEntity = empService.getById(addEmp.getEmpId());
            SysDeptEntity deptEntity = sysDeptService.getById(empEntity.getDeptId());
            addEmp.setEmpName(empEntity.getEmpName());
            addEmp.setDeptName(deptEntity.getName());
        }
        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAddEmp(EmpEntity emp) {
        /*保存员工基本信息到员工表*/
        //添加的员工为在在岗状态
        emp.setStatus(0);
        empService.save(emp);
        /*保存员工站段信息到站段表*/
        saveAttachmentEmp(emp.getEmpId());
        /*保存新进操作信息到新进操作表*/
        AddEmp addEmp =new AddEmp();
        //用户名
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        addEmp.setEmpId(emp.getEmpId());
        addEmp.setIdCard(emp.getIdCard());
        addEmp.setOperator(username);
        addEmp.setDeptId(emp.getDeptId());
        addEmp.setJoinType(emp.getJoinType());
        addEmp.setDate(new Date());
        this.save(addEmp);
          /*人员新进后，更新缓存*/
        Set<String> keys = redisTemplate.keys("*");
        redisTemplate.delete(keys);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAddEmpList(List<EmpEntity> emp) {
        //用户名
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        for(EmpEntity empEntity:emp)
        {
            /*保存员工基本信息到员工表*/
            //添加的员工为在岗状态
            empEntity.setStatus(0);
            empService.save(empEntity);
            /*保存员工站段信息到站段表*/
            saveAttachmentEmp(empEntity.getEmpId());
            /*保存新进操作信息到新进操作表*/
            AddEmp addEmp =new AddEmp();
            addEmp.setEmpId(empEntity.getEmpId());
            addEmp.setIdCard(empEntity.getIdCard());
            addEmp.setDeptId(empEntity.getDeptId());
            addEmp.setOperator(username);
            addEmp.setJoinType(empEntity.getJoinType());
            addEmp.setDate(new Date());
            this.save(addEmp);
        }
        /*人员新进后，更新缓存*/
        Set<String> keys = redisTemplate.keys("*");
        redisTemplate.delete(keys);
    }

    @Override
    public AddEmp queryAddEmp(Long empId) {
        return baseMapper.queryAddEmp(empId);
    }

    @Override
    @DataFilter(subDept = true,user = false)
    public List<Map> addEmpStatisticsByMonth(Map<String, Object> params) {
        return  baseMapper.addEmpStatisticsByMonth(params);
    }
    public void saveAttachmentEmp(Long empId)
    {
        String parentDept = empService.queryParentDept(empId);
        switch(parentDept)
        {
            case "jwd":
            {
                JWDEmpEntity jwdEntity=new JWDEmpEntity();
                jwdEntity.setEmpId(empId);
                 jwdService.save(jwdEntity);
            }
            break;
            case "kyd":
            {
                KYDEmpEntity kydEntity=new KYDEmpEntity();
                kydEntity.setEmpId(empId);
                kydService.save(kydEntity);
            }
            break;
        }
    }
}
